package com.ropeok.service.serviceimpl;

import com.ropeok.Exception.ServiceException;
import com.ropeok.Utils.MD5;
import com.ropeok.constant.ResultConstant;
import com.ropeok.entity.User;
import com.ropeok.mapper.UserMapper;
import com.ropeok.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 查找用户表
     * @return
     */
    @Override
    public List<User> FindUser() {
        List<User> userList = userMapper.FindAll();
        if(userList.isEmpty()&&userList==null){
            throw new ServiceException(ResultConstant.DATA_NOT_EXIST,"无用户数据");
        }
        return  userList;
    }

    /**
     * 新增用户
     * @param user
     */
    @Override
    public void SaveUser(User user) {
        List<Long> AllUserId = userMapper.FindUserId();
        if (AllUserId.contains(user.getUserid())){
            throw new ServiceException(ResultConstant.ID_IS_EXIST,"用户已存在");
        }
        user.setSalt(MD5.salt());
        user.setPassword(MD5.MD5Encrypt(user.getPassword(),user.getSalt()));
        userMapper.Save(user);
    }
}
